package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostSearchDao;
import ysq.xyg.host.model.po.HostSearch;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostSearchVo;
import ysq.xyg.host.service.HostSearchService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 房产模块-搜索关键字service实现
 *
 * @author shengQiang yu
 * @date 2019-7-16 14:44:32
 */
@Service("hostSearchService")
public class HostSearchServiceImpl extends BaseServiceImpl<HostSearch> implements HostSearchService {

    @Resource
    private HostSearchDao hostSearchDao;

    private HostSearch hostSearch;

    @Autowired
    public void setBaseDao(HostSearchDao hostSearchDao) {
        this.baseDao = hostSearchDao;
    }


    /**
     * 分页查询
     *
     * @param hostSearchVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-16 14:44:32
     */
    public ReturnEntity page(HostSearchVo hostSearchVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<HostSearch> list = acceptList(hostSearchVo);
        PageInfo<HostSearch> page=new PageInfo<HostSearch>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostSearchVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-16 14:44:32
     */
    public ReturnEntity addOrUpdate(HostSearchVo hostSearchVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(hostSearchVo.getId())){
            hostSearch=new HostSearch(currentUser);
            isUpate=false;
        }else{
            hostSearch=selectByPrimaryKey(hostSearchVo.getId());
            if(ObjectUtil.isNull(hostSearch)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            hostSearch.setUpdateTime(new Date());
            hostSearch.setUpdateUser(currentUser.getId());
        }
        hostSearch.setName(hostSearchVo.getName());
        hostSearch.setCityId(hostSearchVo.getCityId());
        hostSearch.setSort(hostSearchVo.getSort());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(hostSearch);
        }else{
            num=insert(hostSearch);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-16 14:44:32
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostSearch hostSearch=selectByPrimaryKey(id);
        if(hostSearch!=null){
            hostSearch.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostSearch.setUpdateTime(new Date());
            hostSearch.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(hostSearch);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }


    /**
     * 获取集合
     *
     * @param hostSearchVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-16 14:44:32
     */
    public List<HostSearch> acceptList(HostSearchVo hostSearchVo){
        Example example = new Example(HostSearch.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(hostSearchVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",hostSearchVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(hostSearchVo.getCityId())){
            criteria.andEqualTo("cityId",hostSearchVo.getCityId());
        }
        if(StringUtil.isNotEmpty(hostSearchVo.getName())){
            criteria.andLike("name","%"+hostSearchVo.getName()+"%");
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return hostSearchDao.selectByExample(example);
    }
}
